package com.by.controller;

import com.by.constant.MessageConstant;
import com.by.entity.PageResult;
import com.by.entity.QueryPageBean;
import com.by.entity.Result;
import com.by.pojo.CheckItem;
import com.by.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
控制层：接受请求，并返回数据
*/

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;


    //@ResponseBody 会将Result pojo 对象转换成json对象
    /**
    * @Description: 新增检查项
            * @Param: * @param checkItem
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/9
            */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){ // @RequestBody:作用json--->pojo
        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
    * @Description: 分页查询数据
            * @Param: * @param queryPageBean
            * @return: com.by.entity.PageResult
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkItemService.findPage(queryPageBean);
    }

    /**
    * @Description: 根据数据删除检查项
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    /**
    * @Description: 回显数据
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
    * @Description: 编辑检查项
            * @Param: * @param checkItem
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * @Description: 查询所有检查项
     * @Param: * @param id
     * @return: com.by.entity.Result
     * @Author: GP
     * @Date: 2024/1/10
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }



}
